package javaclass.trouble;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Test2 {
    private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1,
            1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(500),
            new ThreadPoolExecutor.CallerRunsPolicy());
    private static ThreadPoolExecutor threadPool2 = new ThreadPoolExecutor(20, 20,
            1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5000),
            new ThreadPoolExecutor.CallerRunsPolicy());
    
    public static void main(String[] args) {
        MyRunable2 myRunable2 = new MyRunable2();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i =1;i<1000;i++) {
                    threadPool2.execute(myRunable2);
                }
            }
        }).start();
        Random rd = new Random();
        MyRunable myRunable = new MyRunable(rd.nextInt(10000));
        threadPool.execute(myRunable);
    }
}
class MyRunable implements Runnable{
    private int a;
    public MyRunable(int a) {
        this.a = a;
    }
    @Override
    public void run() {
        ServiceA sa = ServiceA.get(a);
        System.out.println(sa);
    }
}
class MyRunable2 implements Runnable{
    @Override
    public void run() {
        ServiceA.go();
    }
}